<?php

class Webmix_Model_DbTable_Musicalbums extends Engine_Db_Table {
	protected $_rowClass = 'Webmix_Model_Musicalbum';

  public function setPhoto($photo)
  { 
    $viewer = Engine_Api::_()->user()->getViewer();
    if( $photo instanceof Zend_Form_Element_File ) {
      $file = $photo->getFileName();
      $fileName = $file;
    } else if( $photo instanceof Storage_Model_File ) {
      $file = $photo->temporary();
      $fileName = $photo->name;
    } else if( $photo instanceof Core_Model_Item_Abstract && !empty($photo->file_id) ) {
      $tmpRow = Engine_Api::_()->getItem('storage_file', $photo->file_id);
      $file = $tmpRow->temporary();
      $fileName = $tmpRow->name;
    } else if( is_array($photo) && !empty($photo['tmp_name']) ) {
      $file = $photo['tmp_name'];
      $fileName = $photo['name'];
    } else if( is_string($photo) && file_exists($photo) ) {
      $file = $photo;
      $fileName = $photo;
    } else {
      throw new User_Model_Exception('invalid argument passed to setPhoto');
    }

    if( !$fileName ) {
      $fileName = $file;
    }
    
    $imageSize = getimagesize($file);
    $imageWidth = $imageSize[0];
    $imageHeight = $imageSize[1];
    
    
    $name = basename($file);
    $extension = ltrim(strrchr($fileName, '.'), '.');
    $base = rtrim(substr(basename($fileName), 0, strrpos(basename($fileName), '.')), '.');
    $path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'temporary';
    $params = array(
      'parent_type' => 'webmix_musicalbum',
      'parent_id' => '0',
      'user_id' => $viewer->getIdentity(),
      'name' => $fileName,
    );

    // Save
    $filesTable = Engine_Api::_()->getDbtable('files', 'storage');

    
    // Resize image (main)
    $mainPath = $path . DIRECTORY_SEPARATOR . $base . '_m.' . $extension;
    $image = Engine_Image::factory();
    $image->open($file)
      ->resize(720, 720)
      ->write($mainPath)
      ->destroy();
      
    // Resize image (normal)
    $normalPath = $path . DIRECTORY_SEPARATOR . $base . '_in.' . $extension;
    if($imageWidth > 83 || $imageHeight> 83){
        if($imageHeight > 83) $imageHeight = 83;
        if($imageWidth > 83) $imageWidth = 83;
        $image = Engine_Image::factory(); 
        $image->open($file)
          ->resize($imageHeight, $imageWidth)
          ->write($normalPath)
          ->destroy();
    }
    else{
       
        copy($file,$normalPath);
        copy($file,$mainPath);
    }
    
    
    // Store
    try {
      $iMain = $filesTable->createFile($mainPath, $params);
      $iIconNormal = $filesTable->createFile($normalPath, $params);
      
      $iMain->bridge($iIconNormal, 'thumb.normal');
    } catch( Exception $e ) {
      // Remove temp files
      @unlink($mainPath);
      @unlink($normalPath);
      // Throw
      if( $e->getCode() == Storage_Model_DbTable_Files::SPACE_LIMIT_REACHED_CODE ) {
        throw new Album_Model_Exception($e->getMessage(), $e->getCode());
      } else {
        throw $e;
      }
    }
    
    // Remove temp files
    @unlink($mainPath);
    @unlink($normalPath);

//    // Update row
//    $this->modified_date = date('Y-m-d H:i:s');
//    $this->file_id = $iMain->file_id;
//    $this->save();

    // Delete the old file?
    if( !empty($tmpRow) ) {
      $tmpRow->delete();
    }

    return $iMain->file_id;
  } 
  
  public function getCompanybyWebmix($webmix_id){
    $blockTable = Engine_Api::_()->getItemTable('webmix_block');
    $select = $blockTable->select()
                        ->from($blockTable->info('name'),array('musicalbum_id'))
                        ->where('webmix_id = ?',$webmix_id)
                        ; 
    $companies_id = $blockTable->fetchAll($select)->toArray();
    $companyArray = array();   
  
    $i = 0;  
    foreach($companies_id as $company_id){
        $company = Engine_Api::_()->getItem('webmix_musicalbum',$company_id['musicalbum_id']);
        $companyArray[$i] = $company;
        $i++;
    }  
    return $companyArray;
  }   
  
  public function getCompanyIDbyWebmix($webmix_id){
        $blockTable = Engine_Api::_()->getItemTable('webmix_block');
        $select = $blockTable->select()
                            ->where('webmix_id = ?',$webmix_id)
                            ; 
        $blocks = $blockTable->fetchAll($select);
        $companyArray = array();   
 
        $companyArray[] = 0;
        foreach($blocks as $block){
            $companyArray[] = $block->company_id;
        }  
        return $companyArray;
  }
 }